<?php

use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator backend\modules\gii\generators\tree\Generator */

/* @var $class ActiveRecordInterface */
$class = $generator->modelClass;
$pks = $class::primaryKey();

$modelClass = StringHelper::basename($generator->modelClass);
$treeModelClass = StringHelper::basename($generator->treeModelClass);
if ($modelClass === $treeModelClass)
{
    $modelAlias = $modelClass . 'Model';
}
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

echo "<?php\n";

?>

namespace <?= StringHelper::dirname(ltrim($generator->treeModelClass, '\\')) ?>;

use common\traits\TreeModelTrait;
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * <?= $treeModelClass ?> represents the model behind the tree form about `<?= $generator->modelClass ?>`.
 */
class <?= $treeModelClass ?> extends <?php echo (isset($modelAlias) ? $modelAlias : $modelClass) . "\n" ?>
{

    use TreeModelTrait {
        rules as treeRule;
        behaviors as treeBehaviors;
        attributeLabels as treeAttributeLabels;
    }

    /**
     * @var string the classname for the TreeQuery that implements the NestedSetQueryBehavior.
     * If not set this will default to `kartik\tree\models\TreeQuery`.
     */
    public static $treeQueryClass;

    /**
     * @var bool whether to HTML encode the tree node names. Defaults to `true`.
     */
    public $encodeNodeNames = true;

    /**
     * @var bool whether to HTML purify the tree node icon content before saving.
     * Defaults to `true`.
     */
    public $purifyNodeIcons = true;

    /**
     * @var array activation errors for the node
     */
    public $nodeActivationErrors = [];

    /**
     * @var array node removal errors
     */
    public $nodeRemovalErrors = [];

    /**
     * @var bool attribute to cache the `active` state before a model update. Defaults to `true`.
     */
    public $activeOrig = true;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return parent::tableName();
    }

    /**
     * Note overriding isDisabled method is slightly different when
     * using the trait. It uses the alias.
     */
    public function isDisabled()
    {
        return $this->parentIsDisabled();
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, $this->treeBehaviors());
        return $behaviors;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, $this->treeAttributeLabels());
        return $attributeLabels;
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        $rules = $this->treeRule();
        return $rules;
    }
}
